package com.wxzz.elearing.comment.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.dto.ImproveCommentDto;
import com.wxzz.elearing.comment.entity.ImproveComment;
import com.wxzz.elearing.comment.service.IImproveCommentService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wx-improve-comment")
@Api(tags = "改善问答类接口")
public class WxImproveCommentController {

    @Autowired
    private IImproveCommentService iImproveCommentService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "改善问答评论(小程序)", notes = "改善问答评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveComment", method = RequestMethod.POST)
    public ReturnVO saveComment(@RequestBody ImproveComment comment, @RequestHeader(value = "token") String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject == null) {
            throw new ServiceException("已失效");
        }
        comment.setIcon(jsonObject.getString("icon"));
        comment.setNickname(jsonObject.getString("nickname"));
        comment.setWxUserId(jsonObject.getLong("id"));
        return iImproveCommentService.saveComment(comment);
    }

    @ApiOperation(value = "根据改善id列出所有评论(小程序)", notes = "根据改善id列出所有评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listComment", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<ImproveComment>>> listComment(@RequestBody ImproveCommentDto skillCommentDto) {
        return iImproveCommentService.listComment(skillCommentDto);
    }

    @ApiOperation(value = "根据改善id删除评论(小程序)", notes = "根据改善id删除评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "deleteComment/{uid}", method = RequestMethod.POST)
    public ReturnVO deleteComment(@PathVariable String uid) {
        return iImproveCommentService.deleteComment(uid);
    }

}
